package com.moweilin.folded_binding.controllers;

import com.moweilin.folded_binding.FoldedBinder;
import com.moweilin.folded_binding.events.BindingEvent;
import javafx.animation.ParallelTransition;
import javafx.animation.RotateTransition;
import javafx.animation.ScaleTransition;
import javafx.animation.TranslateTransition;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Polyline;
import javafx.stage.FileChooser;
import javafx.stage.Window;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * Created by moweilin on 2017/1/16.
 */
public class MainAppController implements Initializable{
    @FXML
    private Button openButton;
    @FXML
    private VBox root;
    @FXML
    private HBox subContainer;
    private FoldedBinder binder;

    @FXML
    private TitledPane ignoreListPane;
    @FXML
    private CheckBox coverChecker;
    @FXML
    private Node expandIcon;


    @FXML
    private Spinner<Integer> fromSpinner;

    @FXML
    private Spinner<Integer> toSpinner;

    private void initSub(){

    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        ignoreListPane.setCollapsible(false);
//        binder = new FoldedBinder();
        binder.sourceFileProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                subContainer.setDisable(newValue == null);
                SpinnerValueFactory<Integer> svf = new SpinnerValueFactory.IntegerSpinnerValueFactory(1, binder.getPageCount());
                fromSpinner.setValueFactory(svf);
                svf.setValue(10);
            }
        });
        root.addEventHandler(BindingEvent.FILE_CHANGED, event -> {
            File sourceFile = event.getSourceFile();
            if(sourceFile != null){
                binder.setSourceFile(sourceFile.getAbsolutePath());
                System.out.println("File changed");
            }

        });
    }

    @FXML
    void openFile(ActionEvent event) {
        Window window = openButton.getScene().getWindow();
        final FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("View Pictures");
        fileChooser.setInitialDirectory(
                new File(System.getProperty("user.home"))
        );
        fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("PDF", "*.pdf"));
        File file = fileChooser.showOpenDialog(window);
        if(file != null){
            System.out.println(file.getAbsolutePath());
            BindingEvent bindingEvent = new BindingEvent(file,BindingEvent.FILE_CHANGED);
            openButton.fireEvent(bindingEvent);
        }

    }
    @FXML
    void toggleIgnoreList() {
//        ignoreListPane.isExpanded()

        boolean toExpand = !ignoreListPane.isExpanded();
        ignoreListPane.setExpanded(toExpand);
        if(toExpand) {
            ignoreListPane.requestFocus();
        }
        RotateTransition rotateTransition = new RotateTransition();
        rotateTransition.setNode(expandIcon);
        double maxAngle = 90;
        double fromAngle = toExpand? maxAngle:0;
        double toAngle = toExpand? 0:maxAngle;
        rotateTransition.setFromAngle(fromAngle);
        rotateTransition.setToAngle(toAngle);


        ScaleTransition scaleTransition = new ScaleTransition();
        scaleTransition.setNode(ignoreListPane);
        double scaleFrom = toExpand?0:1;
        scaleTransition.setFromX(scaleFrom);
        scaleTransition.setFromY(scaleFrom);
        double scaleTo = toExpand?1:0;
        scaleTransition.setToX(scaleTo);
        scaleTransition.setToY(scaleTo);

        TranslateTransition translateTransition = new TranslateTransition();
        translateTransition.setNode(ignoreListPane);
        double transFromX = toExpand?ignoreListPane.getWidth():0;
        translateTransition.setFromX(transFromX);
        double transFromY = toExpand?-ignoreListPane.getHeight():0;
        translateTransition.setFromY(transFromY);
        double transToX = toExpand?0:ignoreListPane.getWidth();
        translateTransition.setToX(transToX);
        double transToY = toExpand?0:-ignoreListPane.getHeight();
        translateTransition.setToY(transToY);
        ParallelTransition parallelTransition = new ParallelTransition();
        parallelTransition.getChildren().add(rotateTransition);
        parallelTransition.getChildren().add(scaleTransition);
        parallelTransition.getChildren().add(translateTransition);
        parallelTransition.play();

    }

}

